using System;
using miPics.Common;


public partial class Image_Default : miPics.BasePage.BasePage
{
	protected void Page_Load(object sender, EventArgs e)
	{
		// get querystring values
		short ImageID = ToolKit.ReturnShort(Request.QueryString["id"]);
	
		// get image details into datatable
		miPicsDataAccessTableAdapters.ImageItemTableAdapter ImageItemTableAdapter = new miPicsDataAccessTableAdapters.ImageItemTableAdapter();
		miPicsDataAccess.ImageItemDataTable ImageItemDataTable = ImageItemTableAdapter.GetData(ImageID,this.IsLoggedIn);

		this.ImageLoaded = (ImageItemDataTable.Count == 1);

		// was a image record found?
		if (ImageItemDataTable.Count == 1)
		{
			miPicsDataAccess.ImageItemRow ImageItemRow = (miPicsDataAccess.ImageItemRow)ImageItemDataTable.Rows[0];

			// get the image name
			string ImageName = ToolKit.GetAltImageName(ImageItemRow.Name);

			// set menu task
			Master.SetMenuSectionGallery(
				ImageItemRow.GalleryID,
				ImageItemRow.GalleryName,
				ImageName
			);

			// assign image title/description
			_Title.InnerHtml = ImageName;
			_Description.InnerHtml = ImageItemRow.Description;

			// assign image details
			_Image.Src = "~/_Resource/Image/" + ImageItemRow.Fullsize;
			_Image.Alt = ToolKit.GetAltImageName(ImageItemRow.Name);
			_Image.Width = ImageItemRow.FullsizeWidth;
			_Image.Height = ImageItemRow.FullsizeHeight;

			// assign image count details
			_ImageCount.InnerText =
				"Image: " + ImageItemRow.SequenceActual + "/" +
				this.GalleryImageCount(ImageItemRow.GalleryID).ToString();

			// assign prev/next/back links
			_BackToGallery.HRef = "~/gallery/?id=" + ImageItemRow.GalleryID.ToString();

			if ((ImageItemRow.PrevImageID == ImageID) || (ImageItemRow.NextImageID == ImageID))
			{
				// hide next/prev image links, since there is only a single image in the gallery
				_PrevImageInterface.Visible = _NextImageInterface.Visible = false;
			}
			else
			{
				_PrevImage.HRef = "?id=" + ImageItemRow.PrevImageID.ToString();
				_NextImage.HRef = "?id=" + ImageItemRow.NextImageID.ToString();
			}
		}
	}

	private short GalleryImageCount(short GalleryID)
	{
		miPicsDataAccessTableAdapters.GalleryItemTableAdapter GalleryItemTableAdapter = new miPicsDataAccessTableAdapters.GalleryItemTableAdapter();
		return (short)GalleryItemTableAdapter.GetImageCount(GalleryID, this.IsLoggedIn);
	}

	private bool ImageLoaded
	{
		set
		{
			_ImageCount.Visible = value;
			_Image.Visible = value;
			_GalleryNavigation.Visible = value;
		}
	}
}